const { json } = require("express");
const { sqlPromise } = require("../db/sqlPromise");

exports.getAttributes = async (req, res) => {
  let sql = `select * from sp_attribute where cat_id=? and attr_sel=?`;
  let results = await sqlPromise(sql, [req.params.id, req.query.sel]);

  if (!results.length) return res.send({ msg: "查询为空" });

  res.send({
    data: results,
    meta: {
      msg: "获取成功",
      status: 200,
    },
  });
};

exports.addAttributes = async (req, res) => {
  let sql = "select * from sp_attribute where attr_name=?";
  let results = await sqlPromise(sql, [req.body.attr_name]);
  if (results.length > 0) return res.send({ status: 100, msg: "该参数已存在" });

  let insertSql = `insert into sp_attribute (cat_id,attr_name,attr_sel,attr_vals) values ('${req.params.id}','${req.body.attr_name}','${req.body.attr_sel}','${req.body.attr_vals}')`;
  let insertRes = await sqlPromise(insertSql);
  console.log(insertRes);

  if (insertRes.affectedRows != 1)
    return res.send({ msg: "添加失败 未知错误" });

  results = await sqlPromise(sql, [req.body.attr_name]);

  res.send({
    data: {
      attr_id: results[0].attr_id,
      attr_name: results[0].attr_name,
      cat_id: results[0].cat_id,
      attr_sel: results[0].attr_sel,
      attr_write: results[0].attr_write,
      attr_vals: results[0].attr_vals,
    },
    meta: {
      msg: "创建成功",
      status: 201,
    },
  });
};

exports.delAttributes = async (req, res) => {
    let sql = "DELETE FROM sp_attribute WHERE attr_id = ? and cat_id=?";
    let results = await sqlPromise(sql, [req.params.attrid,req.params.id]);
    if (results.affectedRows != 1)
      return res.send({
        msg: "删除失败",
        status: 100,
      });
  
    res.send({
      data: null,
      meta: {
        msg: "删除成功",
        status: 200,
      },
    });
};

exports.getAttribute = async (req, res) => {
    let sql = "select * from sp_attribute where attr_id = ? and cat_id=?";
    let results = await sqlPromise(sql, [req.params.attrId,req.params.id]);
    if (!results.length) return res.send({ msg: "查询为空" });
    delete results[0].delete_time

  res.send({
    data: results,
    meta: {
      msg: "获取成功",
      status: 200,
    },
  });
};

exports.editAttribute = async (req, res) => {
    let sql1 = `select * from sp_attribute where attr_id='${req.params.attrId}'`;
    let result1 = await sqlPromise(sql1);
    if (!result1.length) return res.send({ status: 100, msg: "此id不存在" });


    let updateSql = `UPDATE sp_attribute SET cat_id='${req.params.id}',attr_name='${req.query.attr_name}',attr_sel='${req.query.attr_sel}',attr_vals='${req.query.attr_vals}' WHERE attr_id='${req.params.attrId}'`;
    let upResult = await sqlPromise(updateSql);
    if (upResult.affectedRows != 1)
      return res.send({ status: 100, msg: "角色编辑失败" });
  
    let endResult = await sqlPromise(sql1);
    delete endResult[0].delete_time
    res.send({
      data: endResult,
      meta: {
        msg: "角色编辑成功",
        status: 200,
      },
    });
};
